<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.roleName" placeholder="请输入推广员姓名" clearable
          style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份类型" prop="identityType">
        <el-select v-model="queryParams.identityType" placeholder="身份类型" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.popularize_identity_type" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="审核状态" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.extension_worker_status" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['popularize:popularize:worker:add']">新增</el-button>
      </el-col>
      <!--
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:role:export']">导出</el-button>
      </el-col>
      -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="rextensionWorkerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="180" />
      <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="手机号" prop="mobile" :show-overflow-tooltip="true" width="110" />
      <el-table-column label="身份证号" prop="idNo" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="身份类型" prop="identityType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.popularize_identity_type" :value="scope.row.identityType" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" prop="status" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.extension_worker_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="区域" prop="area" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="工作" prop="job" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="紧急联系人" prop="emergencyContact" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="紧急联系人手机号" prop="emergencyMobile" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleReview(scope.row)"
            v-show="scope.row.status == 'NEW' || scope.row.status == 'VARY_PARTNER'"
            v-hasPermi="['popularize:popularize:worker:status:edit']">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 审核对话框 -->
    <el-dialog v-dialogDragWidth v-dialogDragHeight v-dialogDrag :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机号" style="width: 200px" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idNo">
              <el-input v-model="form.idNo" placeholder="请输入身份证号码" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker v-model="form.birthday" value-format="yyyy-MM-dd" type="date" style="width: 200px"
                range-separator="-" placeholder="请选择出生日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址">
              <el-input v-model="form.address" type="textarea" style="width: 510px" placeholder="请输入地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份类型" prop="identityType">
              <el-select v-model="form.identityType" placeholder="请选择身份类型" style="width: 200px">
                <el-option v-for="dict in dict.type.popularize_identity_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择身份类型" style="width: 200px">
                <el-option v-for="dict in dict.type.extension_worker_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="紧急联系人" prop="emergencyContact">
              <el-input v-model="form.emergencyContact" placeholder="请输入紧急联系人姓名" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系手机号" prop="emergencyMobile">
              <el-input v-model="form.emergencyMobile" placeholder="请输入紧急联系人手机号" style="width: 200px" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作" prop="job">
              <el-input v-model="form.job" placeholder="请输入工作职位" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区域" prop="area">
              <el-input v-model="form.area" placeholder="请输入所属区域" style="width: 200px" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExtensionWorker, addExtensionWorker, updateExtensionWorker } from "@/api/popularize/extensionWorker";

export default {
  name: "ExtensionWorker",
  dicts: ["popularize_identity_type", "extension_worker_status"],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推广员信息表格数据
      rextensionWorkerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        identityType: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "推广员姓名不能为空", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "推广员手机号不能为空", trigger: "blur" },
        ],
        identityType: [
          { required: true, message: "请选择推广员身份类型", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择审核状态", trigger: "blur" },
        ],
      },
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询推广员信息列表 */
    getList () {
      this.loading = true;
      listExtensionWorker(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.rextensionWorkerList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope () {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset () {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
        (this.form = {
          id: undefined,
          name: undefined,
          mobile: undefined,
          idNo: undefined,
          birthday: undefined,
          remark: undefined,
        });
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.roleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "system/role/export",
        {
          ...this.queryParams,
        },
        `推广员列表_${new Date().getTime()}.xlsx`
      );
    },
    handleAdd(){
      this.open = true;
      this.form = {};
      this.title = "新增推广员信息"
    },
    handleReview(row){
      this.open = true;
      this.form = row;
      this.title = "修改推广员信息"
    },
    submitForm(){
      let _this = this
      if(!this.form.id){
        this.$modal.confirm('确认新增吗？').then(function () {
          addExtensionWorker(_this.form).then(res=>{
            _this.$modal.msgSuccess("新增成功");
            _this.open = false
            _this.getList()
          });
        }).catch(function () {
        });
      } else{
        this.$modal.confirm('确认修改吗？').then(function () {
          updateExtensionWorker(_this.form.id, _this.form).then(res=>{
            _this.$modal.msgSuccess("修改成功");
            _this.open = false
            _this.getList()
          });
        }).catch(function () {
        });
      }
    },
  },
};
</script>
